using BootstrapBlazor.Components;
using JinShun.OA.UI.Dtos.User;
using JinShun.OA.UI.IServer.Post;
using JinShun.OA.UI.IServer.User;
using JinShun.OA.UI.Models;
using JinShun.OA.UI.Services.Navigation;
using Microsoft.AspNetCore.Components;
using System.Diagnostics.CodeAnalysis;
using System.Linq;

namespace JinShun.OA.UI.Components.Pages
{
    public partial class User
    {
        [Inject]
        [NotNull]
        private IUser UserManager { get; set; }

        private async Task<QueryData<UserModel>> OnQueryAsync(QueryPageOptions options)
        {
            List<UserModel> Users = await UserManager.GetUsersAsync();

            // 处理模糊查询
            if (options.Searches.Any())
            {
                Users = (Users.Where(options.Searches.GetFilterFunc<UserModel>(FilterLogic.Or))).ToList();
            }

            //  处理 Filter 高级搜索
            if (options.CustomerSearches.Any() || options.Filters.Any())
            {
                Users = (Users.Where(options.CustomerSearches.Concat(options.Filters).GetFilterFunc<UserModel>())).ToList();
            }

            return await Task.FromResult(new QueryData<UserModel>()
            {
                IsFiltered = true,
                IsSearch = true,
                IsSorted = true,
                Items = (IEnumerable<UserModel>)Users
            });
        }

        private async Task<bool> OnSaveAsync(UserModel item, ItemChangedType changedType)
        {
            if (changedType == ItemChangedType.Add)
            {
                RegisterDto vo = new RegisterDto();
                vo.UserName=item.UserName;
                vo.Password = item.Phone.ToString();
                vo.Phone = item.Phone;
                vo.Code = string.Empty;
                vo.Uuid = string.Empty;
                vo.Name = item.Name;
                await UserManager.CreateUserAsync(vo);
            }
            return true;
        }
    }
}